package cn.buildworld.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Part;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import org.springframework.web.servlet.view.xml.MappingJackson2XmlView;
import cn.buildworld.model.User;
import cn.buildworld.service.UserService;

/**
 *
 * 第一个控制器
 *
 */

@Controller
@RequestMapping("/user")
@SessionAttributes(value={"user_name"},types={User.class})
public class FirstController {


	@Autowired
	private UserService us = null;
	
	@RequestMapping("/index.do")
	public String index(@RequestParam(value="name",defaultValue="michong") String name) throws UnsupportedEncodingException{
		String n = new String(name.getBytes("ISO-8859-1"), "UTF-8");  
		System.out.println(n);
		return "index";
	}
	@RequestMapping("/index2.do")
	public ModelAndView index2() throws Exception{
		
		List<User> selectAll = us.selectAll();
		System.out.println(selectAll.size());
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("user", selectAll);
		modelAndView.setView(new MappingJackson2JsonView());
		return modelAndView;
	}
	
	@RequestMapping("/index3.do")
	public ModelAndView index3() throws Exception{
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("name", "michong");
		modelAndView.setView(new MappingJackson2JsonView());
		
		return modelAndView;
	}
	@RequestMapping("/index4.do")
	public String login(User user){
		
		System.out.println(user);
		ModelAndView view = new ModelAndView();
		view.addObject("user_name", user.getUser_name());
		return "redirect:/index.jsp";
	}
	@RequestMapping("/index5.do")
	public ModelAndView index5() throws Exception{
		
		List<User> selectAll = us.selectAll();
		System.out.println(selectAll);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("user", selectAll);
		modelAndView.setView(new MappingJackson2JsonView());
		return modelAndView;
	}
	@RequestMapping("/upload.do")
	public ModelAndView upload(MultipartFile file){
		ModelAndView view = new ModelAndView();
		view.setView(new MappingJackson2JsonView());
		String fileName = file.getOriginalFilename();
		System.out.println(fileName);
		File f = new File("e:/pic/"+fileName);
		try {
			file.transferTo(f);
			view.addObject("success", true);
			view.addObject("msg", "鏂囦欢涓婁紶鎴愬姛");
		} catch (Exception e) {
			// TODO: handle exception
			view.addObject("success", false);
			view.addObject("msg", "鏂囦欢涓婁紶澶辫触");
		}
		
		return view; 
	}
	
	@RequestMapping("/upload2.do")
	public ModelAndView upload2(Part file){
		ModelAndView view = new ModelAndView();
		view.setView(new MappingJackson2JsonView());
		String fileName = file.getName();
		System.out.println(fileName);
		try {
			file.write("e:/pic/"+fileName);
			view.addObject("success", true);
			view.addObject("msg", "鏂囦欢涓婁紶鎴愬姛");
		} catch (Exception e) {
			// TODO: handle exception
			view.addObject("success", false);
			 view.addObject("msg", "鏂囦欢涓婁紶澶辫触");
		}
		
		return view; 
	}
	
	@RequestMapping("index6.do")
	public ModelAndView format(@RequestParam("date") @DateTimeFormat(iso =ISO.DATE) Date date,
							   @RequestParam("money") @NumberFormat(pattern = "#,###.##") Double money){
		
		ModelAndView mv = new ModelAndView();
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = sim.format(date);
		mv.addObject("date", format);
		mv.addObject("moeny", money);
		mv.setView(new MappingJackson2JsonView());
		return mv;
	}
	
}
